<?php

class Angsuran extends Controller {

	function Angsuran()
	{
		parent::Controller();	
		$this->load->library('auth');
		$this->auth->check_user_authentification();
		$this->load->model('angsuran_model');		
		$this->load->helper('indodate');
		$this->load->helper('finance');
		$this->load->library('fpdf');
	}
	
	function index()
	{
		$data['title'] = "Daftar Angsuran";
		$data['main_content'] = 'angsuran/display';
		//$date = date("d-m-y");
		//$date = date("d-m-y");
		
		//$data['cobacuy'] = $this->angsuran_model->hit_bunga();
				
		$data['angsuran_data'] = $this->angsuran_model->get_all_data();
		//$this->angsuran_model->hit_bunga();
		
		//$data['cobacuy2'] = $this->angsuran_model->get_all_data();		
		$this->load->view('layout/template', $data);
	}
	
	function simp_bunga()
	{
		$this->angsuran_model->update_bunga();
		$data['title'] = "Daftar Bunga";
		$data['main_content'] = 'angsuran/display_simp_bunga';
		$data['angsuran_data'] = $this->angsuran_model->get_all_data();		
		$this->load->view('layout/template', $data);
	}	
	
	function add()
	{
		$data['title'] = "Tambah Angsuran";
		$data['main_content'] = 'angsuran/form';
		$data['act'] = 'add';
		$data['form_act'] = 'insert';
		$data['angsuran_data'] = FALSE;
		$data['new_id'] = $this->angsuran_model->get_last_id();
		$this->load->view('layout/template', $data);
	}

	function view()
	{
		$id = $this->uri->segment(3);
		$data['title'] = "Lihat Angsuran";
		$data['main_content'] = 'angsuran/form';
		$data['act'] = 'view';
		$data['form_act'] = '';
		$data['angsuran_data'] = $this->angsuran_model->get_data_by_id($id);
		$this->load->view('layout/template', $data);
	}	

	function edit()
	{
		$id = $this->uri->segment(3);
		$data['title'] = "Edit Angsuran";
		$data['main_content'] = 'angsuran/form';
		$data['act'] = 'edit';
		$data['form_act'] = 'update/'.$id;
		$data['angsuran_data'] = $this->angsuran_model->get_data_by_id($id);
		$this->load->view('layout/template', $data);		
	}
	
	function cetak_buku()
	{		
		$this->load->library('fpdf');
		define('FPDF_FONTPATH',$this->config->item('fonts_path'));
		$this->fpdf->FPDF("P","cm","letter");
		$id = $this->uri->segment(3);
		//$data['wajib_pajak_data'] = $this->pajak_model->get_data();	
		$data['laporan_data'] = $this->angsuran_model->get_data_by_id($id);	
		$data['saldo_data'] = $this->angsuran_model->get_saldo_by_id($id);	
		$this->load->view('angsuran/cetak_buku', $data);		
	}
	
	function cetak_validasi()
	{		
		$this->load->library('fpdf');
		define('FPDF_FONTPATH',$this->config->item('fonts_path'));
		$this->fpdf->FPDF("P","cm","letter");
		$id = $this->uri->segment(3);	
		$data['laporan_data'] = $this->angsuran_model->get_data_by_id($id);	
		$data['saldo_data'] = $this->angsuran_model->get_saldo_by_id($id);	
		$this->load->view('angsuran/cetak_validasi', $data);		
	}
	
	function cetak_perjanjian_gadaian()
	{		
		$this->load->library('fpdf');
		define('FPDF_FONTPATH',$this->config->item('fonts_path'));
		$this->fpdf->FPDF("P","cm","letter");
		$id = $this->uri->segment(3);	
		$data['laporan_data'] = $this->angsuran_model->get_data_by_id($id);	
		$data['saldo_data'] = $this->angsuran_model->get_saldo_by_id($id);	
		$this->load->view('angsuran/cetak_perjanjian_gadaian', $data);		
	}
	
	function popup()
	{
		$data['angsuran_data'] = $this->angsuran_model->get_all_data();
		$this->load->view('angsuran/popup', $data);
	}
	
	function insert()
	{
		if (!$this->_angsuran_validation())
		{
			$this->session->set_userdata('ERRMSG_ARR', validation_errors());
			$this->add();
		}
		else
		{
			$this->angsuran_model->fill_data();
			//Cek Akun
			if(!$this->angsuran_model->check_name())
			{
				$this->session->set_userdata('ERRMSG_ARR', 'No Transaksi telah digunakan');
				$this->add();
			}
			//Insert Data
			elseif($this->angsuran_model->insert_data()) 
			{
				$this->session->set_userdata('SUCCESSMSG', 'Transaksi baru sukses ');
				redirect('angsuran');
			}
		}
	}

	function update()
	{
		$id = $this->uri->segment(3);
		if (!$this->_angsuran_validation())
		{
			$this->session->set_userdata('ERRMSG_ARR', validation_errors());
			$this->edit();
		}
		else
		{
			$this->angsuran_model->fill_data();
			//Cek Akun
			if(!$this->angsuran_model->check_name($id))
			{
				$this->session->set_userdata('ERRMSG_ARR', 'No Transaksi telah digunakan');
				$this->edit();
			}		
			//Update Data
			elseif($this->angsuran_model->update_data($id))
			{
				$this->session->set_userdata('SUCCESSMSG', 'Update Transaksi sukses ');
				redirect('angsuran');
			}
		}
	}

	function delete()
	{
		$id = $this->input->post('id');
		$angsuran_data = $this->angsuran_model->get_data_by_id($id);
		if($this->_check_jurnal_exist($id))
		{
			$msg = 'E#angsuran '.$angsuran_data['no_transaksi'].' tidak dapat dihapus karena telah dipakai di jurnal.';
		}
		else
		{
			if($this->angsuran_model->delete_data($id))
			{
				$msg = 'S#angsuran '.$angsuran_data['no_transaksi'].' telah dihapus.';
			}
			else
			{
				$msg .= 'E#Terjadi kesalahan dalam menghapus data transaksi '.$angsuran_data['no_transaksi'].'. Harap coba lagi.';
			}
		}
		echo $msg;
	}
	
	function _check_jurnal_exist($id)
	{
		$this->load->model('jurnal_model');
		$this->jurnal_model->set_project(0,$id);
		$journal_data = $this->jurnal_model->get_data();
		if ($journal_data)
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}

	function _angsuran_validation()
	{
		$this->form_validation->set_rules('no_transaksi', 'No Transaksi', 'trim|required');
		$this->form_validation->set_rules('no_rekening', 'No Rekening', 'trim|required');
		$this->form_validation->set_rules('tgl_reg', 'Tanggal Reg', 'trim|required');
		//$this->form_validation->set_rules('biaya', 'Perkiraan Biaya', 'trim|is_natural');
	
		return $this->form_validation->run();
	}

}
/* End of file angsuran.php */
/* Location: ./application/controllers/angsuran.php */
